package com.fjnldx.ks.service;

import com.fjnldx.ks.pojo.domain.ProcessSheet;
import com.fjnldx.ks.pojo.domain.User;
import com.fjnldx.ks.mapper.MesForthMapper;
import com.fjnldx.ks.pojo.mesDoMain.*;
import com.fjnldx.ks.pojo.request.GydReq2;
import com.fjnldx.ks.pojo.request.GydReq3;
import com.fjnldx.ks.pojo.response.Result;
import com.fjnldx.ks.pojo.response.Result2;
import com.fjnldx.ks.utils.Common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MesForthService implements Serializable {
    @Autowired
    MesForthMapper mesForthMapper;
    @Autowired
    Common common;
    @Autowired
    MesForthService mesForthService;
    private static final Logger LOGGER = LoggerFactory.getLogger(MesForthService.class);

    public Result uploadExcelFile(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("文件上传失败，请选择文件");
        }
        String fileName = file.getOriginalFilename();
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
        // && !fileExt.equals("xlsx")
        if (!fileExt.equals("xls")) {
            return Result.error("文件格式错误，上传失败");
        }
        // 文件格式正确，继续执行
        String userDir = System.getProperty("user.dir");
        String filePath = userDir + "/src/main/resources/WEB-INF/files/";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            return mesForthService.importExcelToDataBase(filePath + fileName);
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return Result.error("上传失败！");
    }

    /**
     * 导入工艺单变更Excel至数据库
     */
    public Result importExcelToDataBase(String filePath) {
        Boolean flag = true;
        List<ProcessSheet> allByExcel = common.getAllByExcel(filePath);
        for (ProcessSheet processSheet : allByExcel) {
            // 判断数据库中是否存在该工艺单
            boolean exist = common.isExist(processSheet.getSpecnum());
            // 如果存在就更新
            if (exist) {
                int i = mesForthMapper.updateGyd(processSheet);
                if (i != 1 && i != 0) {
                    flag = false;
                }
            } else {
                int i = mesForthMapper.insertProcessSheet(processSheet);
                if (i != 1) {
                    flag = false;
                }
            }
        }
        if (!flag) {
            return Result.error();
        }
        return Result.success("导入Excel成功");
    }

    /**
     * 获取各工序工艺单
     */
    public Result2 getMesGyd(GydReq2 gydReq) {
        Result2 result = new Result2();
        switch (gydReq.getGongxu()) {
            case "1": // 并条1
                List<MesGydBingtiao1> mesGydBingtiao1 = mesForthMapper.getMesGydBingtiao1(gydReq);
                Integer countMesGydBingtiao1 = mesForthMapper.getCountMesGydBingtiao1();
                result = common.getResult(mesGydBingtiao1, countMesGydBingtiao1);
                break;
            case "2": // 并条2
                List<MesGydBingtiao2> mesGydBingtiao2 = mesForthMapper.getMesGydBingtiao2(gydReq);
                Integer countMesGydBingtiao2 = mesForthMapper.getCountMesGydBingtiao2();
                result = common.getResult(mesGydBingtiao2,countMesGydBingtiao2);
                break;
            case "3": // 并条3
                List<MesGydBingtiao3> mesGydBingtiao3 = mesForthMapper.getMesGydBingtiao3(gydReq);
                Integer countMesGydBingtiao3 = mesForthMapper.getCountMesGydBingtiao3();
                result = common.getResult(mesGydBingtiao3,countMesGydBingtiao3);
                break;
            case "4": // 梳棉
                List<MesGydShumian> mesShumian = mesForthMapper.getMesShumian(gydReq);
                Integer countMesGydShumian = mesForthMapper.getCountMesGydShumian();
                result = common.getResult(mesShumian,countMesGydShumian);
                break;
            case "5": // 细纱
                List<MesGydXisha> mesXisha = mesForthMapper.getMesXisha(gydReq);
                Integer countMesGydXisha = mesForthMapper.getCountMesGydXisha();
                result = common.getResult(mesXisha,countMesGydXisha);
                break;
            case "6": // 粗纱
                List<MesGydChusha> mesGydChuSha = mesForthMapper.getMesGydChuSha(gydReq);
                Integer countMesGydChuSha = mesForthMapper.getCountMesGydChuSha();
                result = common.getResult(mesGydChuSha,countMesGydChuSha);
                break;
            default:
                break;
        }
        return result;
    }
    /**
     * 删除各工序工艺单
     */
    public Result deleteMesGyd(GydReq3 gydReq3) {
        switch (gydReq3.getGongxu()) {
            case "1": // 并条1
                mesForthMapper.deleteMesGydBingTiao1(gydReq3);
                break;
            case "2": // 并条2
                mesForthMapper.deleteMesGydBingTiao2(gydReq3);
                break;
            case "3": // 并条3
                mesForthMapper.deleteMesGydBingTiao3(gydReq3);
                break;
            case "4": // 梳棉
                mesForthMapper.deleteMesGydShuMian(gydReq3);
                break;
            case "5": // 细纱
                mesForthMapper.deleteMesGydXiSha(gydReq3);
                break;
            case "6": // 粗纱
                mesForthMapper.deleteMesGydCuSha(gydReq3);
                break;
            default:
                break;
        }
        return Result.success();
    }
    /**
     * 更新工艺单--并条1
     */
    public Result updateMesGydBingTiao1(MesGydBingtiao1 mesGydBingtiao1) {
        mesForthMapper.updateMesGydBingTiao1(mesGydBingtiao1);
        return Result.success();
    }
    /**
     * 更新工艺单--并条2
     */
    public Result updateMesGydBingTiao2(MesGydBingtiao2 mesGydBingtiao2) {
        mesForthMapper.updateMesGydBingTiao2(mesGydBingtiao2);
        return Result.success();
    }
    /**
     * 更新工艺单--并条3
     */
    public Result updateMesGydBingTiao3(MesGydBingtiao3 mesGydBingtiao3) {
        mesForthMapper.updateMesGydBingTiao3(mesGydBingtiao3);
        return Result.success();
    }
    /**
     * 更新工艺单--梳棉
     */
    public Result updateMesGydShuMian(MesGydShumian mesGydShumian) {
        mesForthMapper.updateMesGydShuMian(mesGydShumian);
        return Result.success();
    }
    /**
     * 更新工艺单--粗纱
     */
    public Result updateMesGydCuSha(MesGydChusha mesGydChusha) {
        mesForthMapper.updateMesGydCuSha(mesGydChusha);
        return Result.success();
    }
    /**
     * 更新工艺单--细纱
     */
    public Result updateMesGydXiSha(MesGydXisha mesGydXisha) {
        mesForthMapper.updateMesGydXiSha(mesGydXisha);
        return Result.success();
    }

    /**
     * 插入工艺单数据并条1
     */
    public Result insertMesGydBingTiao1(MesGydBingtiao1 mesGydBingtiao1) throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        Date time = simpleDateFormat.parse(format);
        mesGydBingtiao1.setTime(time);
        mesForthMapper.insertMesGydBingTiao1(mesGydBingtiao1);
        return Result.success();
    }
    /**
     * 插入工艺单数据并条2
     */
    public Result insertMesGydBingTiao2(MesGydBingtiao2 mesGydBingtiao2) throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        Date time = simpleDateFormat.parse(format);
        mesGydBingtiao2.setTime(time);
        mesForthMapper.insertMesGydBingTiao2(mesGydBingtiao2);
        return Result.success();
    }
    /**
     * 插入工艺单数据并条3
     */
    public Result insertMesGydBingTiao3(MesGydBingtiao3 mesGydBingtiao3) throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        Date time = simpleDateFormat.parse(format);
        mesGydBingtiao3.setTime(time);
        mesForthMapper.insertMesGydBingTiao3(mesGydBingtiao3);
        return Result.success();
    }
    /**
     * 插入工艺单数据梳棉
     */
    public Result insertMesGydShumian(MesGydShumian mesGydShumian) throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        Date time = simpleDateFormat.parse(format);
        mesGydShumian.setTime(time);
        mesForthMapper.insertMesGydShumian(mesGydShumian);
        return Result.success();
    }
    /**
     * 插入工艺单数据粗纱
     */
    public Result insertMesGydChusha(MesGydChusha mesGydChusha) throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        Date time = simpleDateFormat.parse(format);
        mesGydChusha.setTime(time);
        mesForthMapper.insertMesGydChusha(mesGydChusha);
        return Result.success();
    }
    /**
     * 插入工艺单数据细纱
     */
    public Result insertMesGydXiSha(MesGydXisha mesGydXisha) throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        Date time = simpleDateFormat.parse(format);
        mesGydXisha.setTime(time);
        mesForthMapper.insertMesGydXisha(mesGydXisha);
        return Result.success();
    }

    /**
     * 上传生产计划Excel--细纱
     * @param file
     * @return
     */
    public Result uploadExcelFileScjhXisha(MultipartFile file) {
        Result result = common.uploadExcelFile(file);
        String filePath = result.getMsg();
        File dest = new File(result.getMsg());
        if(!dest.exists()){
            dest.mkdirs();
        }
        try {
            file.transferTo(dest);
            return mesForthService.importExcelToDataBaseScjhXisha(filePath);
        } catch (IOException e) {
            return Result.error();
        }

    }

    /**
     * 导入生产计划Excel至数据库----细纱
     */
    public Result importExcelToDataBaseScjhXisha(String filePath) {
        Boolean flag = true;
        List<MesScjhXisha> allByExcel = common.getAllByExcelScjhXiSha(filePath);
        for (MesScjhXisha mesScjhXisha : allByExcel) {
            // 判断数据库中是否存在该生产计划
            boolean exist = common.isExistScjhXiSha(mesScjhXisha.getScjhbh());
            // 如果存在就更新
            if (exist) {
                int i = mesForthMapper.updateScjhXiSha(mesScjhXisha);
                if (i != 1 && i != 0) {
                    flag = false;
                }
            } else {
                int i = mesForthMapper.insertScjhXiSha(mesScjhXisha);
                if (i != 1) {
                    flag = false;
                }
            }
        }
        if (!flag) {
            return Result.error("导入失败");
        }
        return Result.success();
    }

    /**
     * 上传生产计划Excel--粗纱
     * @param file
     * @return
     */
    public Result uploadExcelFileScjhChusha(MultipartFile file) {
        Result result = common.uploadExcelFile(file);
        String filePath = result.getMsg();
        File dest = new File(result.getMsg());
        if(!dest.exists()){
            dest.mkdirs();
        }
        try {
            file.transferTo(dest);
            return mesForthService.importExcelToDataBaseScjhChusha(filePath);
        } catch (IOException e) {
            return Result.error();
        }

    }

    /**
     * 导入生产计划Excel至数据库----粗纱
     */
    public Result importExcelToDataBaseScjhChusha(String filePath) {
        Boolean flag = true;
        List<MesScjhCusha> allByExcel = common.getAllByExcelScjhCuha(filePath);
        for (MesScjhCusha mesScjhCusha : allByExcel) {
            // 判断数据库中是否存在该生产计划
            boolean exist = common.isExistScjhCuSha(mesScjhCusha.getScjhbh());
            // 如果存在就更新
            if (exist) {
                int i = mesForthMapper.updateScjhCuSha(mesScjhCusha);
                if (i != 1 && i != 0) {
                    flag = false;
                }
            } else {
                int i = mesForthMapper.insertScjhCuSha(mesScjhCusha);
                if (i != 1) {
                    flag = false;
                }
            }
        }
        if (!flag) {
            return Result.error();
        }
        return Result.success();
    }
    /**
     * 上传生产计划Excel--络筒
     * @param file
     * @return
     */
    public Result uploadExcelFileScjhLuoTong(MultipartFile file) {
        Result result = common.uploadExcelFile(file);
        String filePath = result.getMsg();
        File dest = new File(result.getMsg());
        if(!dest.exists()){
            dest.mkdirs();
        }
        try {
            file.transferTo(dest);
            return mesForthService.importExcelToDataBaseScjhLuoTong(filePath);
        } catch (IOException e) {
            return Result.error();
        }

    }

    /**
     * 导入生产计划Excel至数据库----络筒
     */
    public Result importExcelToDataBaseScjhLuoTong(String filePath) {
        Boolean flag = true;
        List<MesScjhLuotong> allByExcel = common.getAllByExcelScjhLuoTong(filePath);
        for (MesScjhLuotong mesScjhLuotong : allByExcel) {
            // 判断数据库中是否存在该生产计划
            boolean exist = common.isExistScjhLuoTong(mesScjhLuotong.getScjhbh());
            System.out.println(exist);
            // 如果存在就更新
            if (exist) {
                int i = mesForthMapper.updateScjhLuoTong(mesScjhLuotong);
                if (i != 1 && i != 0) {
                    flag = false;
                }
            } else {
                int i = mesForthMapper.insertScjhLuoTong(mesScjhLuotong);
                if (i != 1) {
                    flag = false;
                }
            }
        }
        Result result = new Result();
        if (!flag) {
            return Result.error();
        }
        return Result.success();
    }
    //@CachePut(value = "test1")
    @Cacheable(cacheNames = {"test1"})
    public String findUser() {
        User mesUser = mesForthMapper.findUser();
        return mesUser.getUsername();
    }

    @CacheEvict(beforeInvocation = true,cacheNames = {"test1"})
    public void deleteCache(){
        System.out.println("清除缓存");
    }

}
